using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace UserManagementService.Organizations;

/// <summary>
/// 组织管理服务接口
/// </summary>
public interface IOrganizationManagementService
{
    /// <summary>
    /// 创建组织
    /// </summary>
    Task<Organization> CreateAsync(
        string code,
        string name,
        OrganizationType type,
        Guid? parentId = null,
        string? description = null,
        Guid? tenantId = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 更新组织信息
    /// </summary>
    Task<Organization> UpdateAsync(
        Guid id,
        string name,
        string? description = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 删除组织
    /// </summary>
    Task DeleteAsync(Guid id, bool force = false, CancellationToken cancellationToken = default);

    /// <summary>
    /// 移动组织到新的父组织下
    /// </summary>
    Task<Organization> MoveAsync(
        Guid id,
        Guid? newParentId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 启用组织
    /// </summary>
    Task<Organization> EnableAsync(Guid id, CancellationToken cancellationToken = default);

    /// <summary>
    /// 禁用组织
    /// </summary>
    Task<Organization> DisableAsync(Guid id, CancellationToken cancellationToken = default);

    /// <summary>
    /// 添加组织成员
    /// </summary>
    Task<OrganizationMember> AddMemberAsync(
        Guid organizationId,
        Guid userId,
        string? position = null,
        string? description = null,
        bool isManager = false,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 移除组织成员
    /// </summary>
    Task RemoveMemberAsync(
        Guid organizationId,
        Guid userId,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 更新组织成员信息
    /// </summary>
    Task<OrganizationMember> UpdateMemberAsync(
        Guid organizationId,
        Guid userId,
        string? position = null,
        string? description = null,
        bool? isManager = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取组织成员列表
    /// </summary>
    Task<List<OrganizationMember>> GetMembersAsync(
        Guid organizationId,
        bool includeInactive = false,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取用户所属组织列表
    /// </summary>
    Task<List<Organization>> GetUserOrganizationsAsync(
        Guid userId,
        bool includeInactive = false,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 获取组织树
    /// </summary>
    Task<List<Organization>> GetOrganizationTreeAsync(
        Guid? rootId = null,
        int maxDepth = 10,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 检查组织编码是否可用
    /// </summary>
    Task<bool> IsCodeAvailableAsync(
        string code,
        Guid? excludeId = null,
        Guid? tenantId = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 批量导入组织
    /// </summary>
    Task<OrganizationImportResult> ImportOrganizationsAsync(
        List<OrganizationImportDto> organizations,
        Guid? tenantId = null,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 导出组织数据
    /// </summary>
    Task<List<OrganizationExportDto>> ExportOrganizationsAsync(
        Guid? rootId = null,
        bool includeMembers = false,
        CancellationToken cancellationToken = default);

    /// <summary>
    /// 同步组织结构
    /// </summary>
    Task SyncOrganizationStructureAsync(
        Guid organizationId,
        CancellationToken cancellationToken = default);
}

/// <summary>
/// 组织导入DTO
/// </summary>
public class OrganizationImportDto
{
    public string Code { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public OrganizationType Type { get; set; }
    public string? ParentCode { get; set; }
    public string? Description { get; set; }
    public string? ContactPerson { get; set; }
    public string? ContactPhone { get; set; }
    public string? ContactEmail { get; set; }
    public string? Address { get; set; }
}

/// <summary>
/// 组织导入结果
/// </summary>
public class OrganizationImportResult
{
    public int TotalCount { get; set; }
    public int SuccessCount { get; set; }
    public int FailureCount { get; set; }
    public List<string> ErrorMessages { get; set; } = new();
}

/// <summary>
/// 组织导出DTO
/// </summary>
public class OrganizationExportDto
{
    public Guid Id { get; set; }
    public string Code { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    public OrganizationType Type { get; set; }
    public string? ParentCode { get; set; }
    public string? Description { get; set; }
    public bool IsEnabled { get; set; }
    public DateTime CreationTime { get; set; }
    public int MemberCount { get; set; }
    public List<string> MemberNames { get; set; } = new();
}